/**
 * @file ui_main.c
 * @author your name (you@domain.com)
 * @brief ui main
 * @version 0.1
 * @date 2023-06-03
 * 
 * @copyright Copyright (c) 2023
 * 
 */

#include "ui_main.h"
#include "evdev.h"
#include "fbdev.h"
#include "lvgl.h"
#include "sdl.h"
#include "ui_change.h"
#include "msg_event_config.h"
#include "ui_power_on.h"
#include "screen_gesture.h"
#include "ui_pull_page.h"

#define UI_HOR_RES 240
#define UI_VER_RES 320

static void lvgl_init()
{
    lv_init();

    //display init
    static lv_disp_draw_buf_t disp_buf_1;
    static lv_color_t buf1_1[UI_HOR_RES * UI_VER_RES];
    sdl_init();
    //fbdev_init();
    lv_disp_draw_buf_init(&disp_buf_1, buf1_1, NULL, UI_HOR_RES * UI_VER_RES);

    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.hor_res = UI_HOR_RES;
    disp_drv.ver_res = UI_VER_RES;
    disp_drv.draw_buf = &disp_buf_1;
    disp_drv.flush_cb = sdl_display_flush;   
    lv_disp_drv_register(&disp_drv);

    //input init
    //evdev_init();
    static lv_indev_drv_t indev_drv;
    lv_indev_drv_init(&indev_drv);
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = sdl_mouse_read;
    lv_indev_drv_register(&indev_drv);    

}

static void act_gesture_event_cb(lv_event_t *event) 
{
    lv_dir_t dir = lv_indev_get_gesture_dir(lv_indev_get_act());
    switch(dir) 
    {
        case LV_DIR_LEFT:
             easy_ui_pub_event(TOPIC_LAST_UI, NULL, 0, 0);
        break;
        case LV_DIR_RIGHT:
             easy_ui_pub_event(TOPIC_NEXT_UI, NULL, 0, 0);
        break;
        case LV_DIR_TOP:
             easy_ui_pub_event(TOPIC_DROP_DOWN_UI, NULL, 0, 0);
        break;
        case LV_DIR_BOTTOM:
             easy_ui_pub_event(TOPIC_DROP_UP_UI, NULL, 0, 0);
        break;
        default:
        break;
    }
}

void screen_gesture_cb(SCREEN_GESTURE_TYPE_e type, bool is_release, param_info_t *param)
{
    if(param)
      COMLOG_INFO_LOG("gesture type: %d is_release: %d x: %d y:%d\n", type, is_release, param->point_x, param->point_y);
    else
      COMLOG_INFO_LOG("gesture type: %d is_release: %d\n", type, is_release);
    switch(type)
    {
       case SCREEN_GESTURE_TOP_SWIP:     easy_ui_pub_event(TOPIC_DROP_DOWN_UI, NULL, 0, 0);    break;
       case SCREEN_GESTURE_BOTTOM_SWIP:  easy_ui_pub_event(TOPIC_DROP_UP_UI, NULL, 0, 0);    break;
       case SCREEN_GESTURE_LEFT_SWIP:    easy_ui_pub_event(TOPIC_LAST_UI, NULL, 0, 0);     break;
       case SCREEN_GESTURE_RIGHT_SWIP:   easy_ui_pub_event(TOPIC_NEXT_UI, NULL, 0, 0);    break;
       case SCREEN_GESTURE_TOP_PULL:     pull_page_action((PULL_DIR_e)type, is_release, param->point_x, param->point_y);   break;
       case SCREEN_GESTURE_BOTTOM_PULL:  pull_page_action((PULL_DIR_e)type, is_release, param->point_x, param->point_y);     break;
       case SCREEN_GESTURE_LEFT_PULL:    pull_page_action((PULL_DIR_e)type, is_release, param->point_x, param->point_y);     break;
       case SCREEN_GESTURE_RIGHT_PULL:   pull_page_action((PULL_DIR_e)type, is_release, param->point_x, param->point_y);     break;

       default: break;
    }
}

static void ui_main_listen_act_gesture()
{
   // lv_obj_add_event_cb(lv_scr_act(), act_gesture_event_cb, LV_EVENT_GESTURE, NULL);
   trigger_condition_t trigger_condition = {0};
   trigger_condition.swip_min_pixel = SCREEN_GESTURE_SWIP_MIN_PIXEL;
   trigger_condition.screen_height = lv_obj_get_height(lv_scr_act());
   trigger_condition.screen_width = lv_obj_get_width(lv_scr_act());
   trigger_condition.pull_region_width = SCREEN_GESTURE_PULL_REGION_WIDTH;
   trigger_condition.cb = screen_gesture_cb;
   lv_screen_gesture_init(&trigger_condition);

   lv_obj_t *top_dir_obj = (lv_obj_t *)pull_page_get(PULL_DIR_TOP);
//    lv_obj_t *bot_dir_obj = (lv_obj_t *)pull_page_get(PULL_DIR_BOTTOM);
//    lv_obj_t *left_dir_obj = (lv_obj_t *)pull_page_get(PULL_DIR_LEFT);
//    lv_obj_t *right_dir_obj = (lv_obj_t *)pull_page_get(PULL_DIR_RIGHT);

   
}

int ui_app_init()
{
    if(easy_ui_frame_init() != 0)
    {
        goto FAIL;
    }

    lvgl_init();

    ui_main_listen_act_gesture();

    ui_change_manage();
    
    ui_change_ui(EASY_UI_POWER_ON, false);

    return 0;
FAIL:
    easy_ui_frame_deinit();
    return -1;
}


int ui_app_loop()
{
    static int i = 0;
    static int cnt = 0;
    static int progress = 0;
    easy_ui_frame_loop();
    lv_tick_inc(5);
    lv_task_handler();
    i++;
    if(i % 180 == 0)
    {
        int lvl = cnt;
        easy_ui_pub_event(TOPIC_BAT_LVL, (void *)&lvl, sizeof(lvl), 0);
        cnt++;
        if(cnt >= 5)
        {
           cnt = 0;
        }
           
        easy_ui_pub_event(TOPIC_RUN_UI, NULL, 0, 0);
        
        if(progress <= 100)
        {
            ui_power_on_set_progress(progress);
            progress += 20;
        }
        else
        {
            progress = 100;
        }
    }
    return 0;
}


int ui_app_deinit()
{
    easy_ui_frame_deinit();

    return 0;
}